package com.ruoyi.web.server.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.config.props.AllPathProperties;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ExecUtil;
import com.ruoyi.common.utils.biz.BizModule;
import com.ruoyi.common.utils.biz.PathUtil;
import com.ruoyi.common.utils.biz.ResultSaveModule;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dm.mapper.DataCollectionMapper;
import com.ruoyi.web.server.entity.EwSmsGwMain;
import com.ruoyi.web.server.entity.ResultSave;
import com.ruoyi.web.server.service.EwSmsGwMainService;
import com.ruoyi.web.server.service.ResultSaveService;
import common.*;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plugins.ImportFile;
import plugins.LowAtmosphereCalcNative;

import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 运营商网关参数主表 前端控制器
 * </p>
 *
 * @author liuhl
 * @since 2024-04-12
 */
@Slf4j
@RestController
@RequestMapping("/waive")
@AllArgsConstructor
public class EwSmsGwMainController {



    private EwSmsGwMainService ewSmsGwMainService;

    private AllPathProperties allPath;

    private DataSource dataSource;
    private DataCollectionMapper dataCollectionMapper;

    private ResultSaveService resultSaveService;

    @RequestMapping("/test1")
    public AjaxResult getTestData(Integer pageNo) {
        String uuid = IdUtil.fastUUID().substring(0,10);
        EwSmsGwMain entity = EwSmsGwMain.builder()
                .protocolCode("code-" + uuid)
                .protocolName(uuid)
                .createTime(new Date())
                .build();
        ewSmsGwMainService.save(entity);

        LambdaQueryWrapper<EwSmsGwMain> wrapper = new LambdaQueryWrapper<EwSmsGwMain>()
                .like(EwSmsGwMain::getProtocolCode, "code")
                .orderByDesc(EwSmsGwMain::getCreateTime);

        Page page = new Page(pageNo, 10);
        page = ewSmsGwMainService.page(page, wrapper);
        return AjaxResult.success(page);
    }

    @RequestMapping("/test2")
    public AjaxResult testInject() throws SQLException {
        //获取模式名
        String schemaName ;
        String schema = dataSource.getConnection().getSchema();
        if (StrUtil.isNotBlank(schema)){
            schemaName = schema;
        }else {
            schemaName = dataSource.getConnection().getMetaData().getUserName();
        }
        List<String> tableNames = dataCollectionMapper.selectAllTableNames(schemaName.toLowerCase()); //大写最好
        if (CollUtil.isEmpty(tableNames)){
            return AjaxResult.error("模式: {} 下没有找到任何数据库表");
        }

        List<String> sortedNames = tableNames
                .stream()
                .map(String::toUpperCase)
                .filter(name -> name.contains("SWDATA_OBS_DEN_"))
                .collect(Collectors.toList());

        List<String> finalTables = sortedNames.stream().filter(tableName -> {
            return dataCollectionMapper.exitsRecords(tableName, "2002-04-01 00:00:00", "2002-04-06 00:00:00") > 0;
        }).collect(Collectors.toList());

        if (CollUtil.isEmpty(finalTables)){
            return AjaxResult.error("模式: {} 下没有找到任何符合搜索条件的数据库表");
        }
        List<String> sateNames = finalTables.stream().map(o -> o.replace("SWDATA_OBS_DEN_", "")).collect(Collectors.toList());
        return AjaxResult.success(sateNames);
    }

    @RequestMapping("/test3")
    public AjaxResult test3() {
        CompletableFuture.runAsync(() -> {
            System.out.println("print: 正在运行 run Async...");
            log.info("log: 正在运行 run Async...");
        }).whenComplete((res,exception) -> {
            System.out.println("print: 已执行完");
            log.info("log: 已执行完");
        });
        return AjaxResult.success();
    }


}
